function verifyNotEmpty(inputElm) {
	if (inputElm.value == "") {
		return false;
	}
	
	return true;
	
}

function setImage(root, is_ok) {
	var img = document.createElement('img');
	if (is_ok) {
		img.src = "images/ok.png";
	}
	else {
		img.src = "images/no-ok.png";
	}
	
	
	if (root.hasChildNodes()) {
		root.removeChild(root.firstChild);
	}
	
	root.appendChild(img);
	
}

function verifyUsername() {
	/* use ajax here */
	var name = "username";
	var username = document.getElementById("input_" + name);
	var res = verifyNotEmpty(username);
	setImage(document.getElementById("img_" + name), res);
	return res;
}

function verifyPassword() {
	var name = "pass";
	var pass = document.getElementById("input_" + name);
	res =  verifyNotEmpty(pass);
	setImage(document.getElementById("img_" + name), res);
	return res;
}

function verifyPasswordVerify() {
	var name = "pass_verify";
	var pass = document.getElementById("input_pass");
	var pass_verify = document.getElementById("input_" + name);
	var res;
	if (!verifyNotEmpty(pass_verify)) {
		res = false;
	}
	
	else if (pass.value != pass_verify.value) {
		res = false;
	}
	
	else {
		res = true;
	}
	
	setImage(document.getElementById("img_" + name), res);
	return res;
}

function verifyFirstName() {
	var name = "first_name";
	var first_name = document.getElementById("input_" + name);
	res =  verifyNotEmpty(first_name);
	setImage(document.getElementById("img_" + name), res);
	return res;
}

function verifyLastName() {
	var name = "last_name";
	var last_name = document.getElementById("input_" + name);
	res =  verifyNotEmpty(last_name);
	setImage(document.getElementById("img_" + name), res);
	return res;
}

function verifyPhoneNumber() {
	var name = "phone_number";
	var phone_number = document.getElementById("input_" + name);
	res =  verifyNotEmpty(phone_number) && /^([0-9]|\+|-)+$/.test(phone_number.value);
	setImage(document.getElementById("img_" + name), res);
	return res;
}

function verifyCourseName() {
	var name = "course_name";
	var course_name = document.getElementById("input_" + name);
	res =  verifyNotEmpty(course_name);
	setImage(document.getElementById("img_" + name), res);
	return res;
}

function verifyCourseDescription() {
	var name = "course_description";
	var course_description = document.getElementById("input_" + name);
	res =  verifyNotEmpty(course_description);
	setImage(document.getElementById("img_" + name), res);
	return res;
}


function verifyCourseCredit() {
	var name = "course_credit";
	var course_credit = document.getElementById("input_" + name);
	res =  course_credit.value > 0; 
	setImage(document.getElementById("img_" + name), res);
	return res;
}

function verifyCourseCapacity() {
	var name = "course_capacity";
	var course_capacity = document.getElementById("input_" + name);
	res =  course_capacity.value > 0; 
	setImage(document.getElementById("img_" + name), res);
	return res;
}

function verifyCourseGroupNumber() {
	var name = "course_group";
	var course_group = document.getElementById("input_" + name);
	res =  course_group.value >= 0; 
	setImage(document.getElementById("img_" + name), res);
	return res;
}



function verifyForm() {
	var verify_list = [verifyUsername,
	                   verifyPassword,
	                   verifyPasswordVerify,
	                   verifyFirstName,
	                   verifyLastName,
	                   verifyPhoneNumber
	                   ];
	
	for (var i in verify_list) {
		if (!verify_list[i]()) {
			return false;
		}
	}
	
	var admin_check = document.getElementById("input_is_admin").checked;
	if (admin_check) {
		var admin_secret = prompt("Please enter administrator secret phrase:");
		var form = document.getElementById("register_form");
		var elm = document.createElement('input');
		elm.name = "admin_secret"; 
		elm.type = "hidden";
		elm.value = admin_secret;
		form.appendChild(elm);
		
	}
			
	return true;
}

function verifyUpdateForm() {
	var verify_list = [verifyPassword,
	                   verifyPasswordVerify,
	                   verifyFirstName,
	                   verifyLastName,
	                   verifyPhoneNumber
	                   ];
	
	for (var i in verify_list) {
		if (!verify_list[i]()) {
			return false;
		}
	}
		
	return true;
}

function verifyAddCourseForm() {
	var verify_list = [verifyCourseName,
	                   verifyCourseDescription,
	                   verifyCourseCredit,
	                   verifyCourseCapacity,
	                   verifyCourseGroupNumber	                  
	                   ];
	
	for (var i in verify_list) {
		if (!verify_list[i]()) {
			return false;
		}
	}
			
	return true;
}